export default {
  name: 'hello',
  data: function () {
    return {
      msg: '这是一个书籍的查询系统',
      addBook: false,
      columns1: [
        { title: '书名', key: 'name' },
        { title: '出版年份', key: 'age' },
        { title: '类型', key: 'type' },
        { title: '操作', key: 'action', render (row, column, index) { return `<i-button type="error" size="small" @click="deleteBook(${index})">删除</i-button>` } }
      ],
      books: [
      ],
      oldBooks: [],
      newBook: { name: '', age: '', type: '技术' },
      bookTypeList: [
        { value: '技术', label: '技术' },
        { value: '八卦', label: '八卦' }
      ],
      searchName: ''
    }
  },
  methods: {
    searchBook: function (searchName) {
      if (this.oldBooks.length > 0) {
        let searchBooks = []
        for (let i = 0; i < this.oldBooks.length; i++) {
          if (this.oldBooks[i].name.indexOf(searchName) >= 0) {
            searchBooks.push(this.oldBooks[i])
          }
        }
        if (searchBooks.length === 0) {
          searchBooks.push({ name: '暂无该书籍' })
        }
        this.books = searchBooks
      }
    },
    showAllBook: function () {
      this.books = this.oldBooks
    },
    createBook: function () {
      this.books.push(this.newBook)
      this.newBook = { name: '', age: '', type: '技术' }
      localStorage.setItem('books', JSON.stringify(this.books))
    },
    deleteBook: function (index) {
      this.books.splice(index, 1)
      localStorage.setItem('books', JSON.stringify(this.books))
    },
    add: function () {
      const t = new Date(this.newBook.age)
      this.newBook.age = `${t.getFullYear()}-${('00' + (t.getMonth() + 1)).slice(-2)}-${('00' + (t.getDate())).slice(-2)}`
      this.books.push(this.newBook)
      this.newBook = { name: '', age: '', type: '技术' }
      localStorage.setItem('books', JSON.stringify(this.books))
      this.$Message.info('新增成功')
    },
    cancel () {
      this.$Message.info('取消新增')
    }
  },
  mounted: function () {
    let books = localStorage.getItem('books')
    this.books = books ? JSON.parse(books) : []
    this.oldBooks = books ? JSON.parse(books) : []
  }
}
